package com.cungmua.app.activity;

import android.content.Intent;
import android.os.Bundle;

import com.cungmua.app.R;
import com.cungmua.app.handler.ListDealHandler;
import com.cungmua.app.model.request.SearchDealRequest;
import com.cungmua.app.model.response.ListDealResponse;
import com.cungmua.app.task.SearchDealTask;

/**
 * Created by kelvin.nguyen on 6/24/14.
 */
public class SearchResultsActivity extends DealListActivity implements ListDealHandler {
    private String query="iphone";

    @Override
    public int getLayoutResource() {
        return R.layout.activity_deal_list;
    }

    @Override
    public void updateView() {
        setEmptyView("Tìm thấy 0 khuyến mãi");

        SearchDealRequest request = new SearchDealRequest(getDeviceId(), getToken());
        request.setKeyword(query);
        request.setCityId(getCurrentCity().getId());
        request.setUserId(getCurrentUser().getId());
        request.setPageIndex(currentPage);

        SearchDealTask task = new SearchDealTask(this, this);
        task.execute(request);
    }

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            query = intent.getStringExtra(KEY_SEARCH);
            currentPage = 1;
            updateView();
        }
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        super.loadControls(savedInstanceState);
        //handleIntent(getIntent());
        Bundle b = getIntent().getExtras();
        query = b.getString(KEY_SEARCH);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    @Override
    public void onListDealReceived(ListDealResponse response) {
        onListDealData(response);
    }
}

